package com.pw.ssm.controller;

import com.pw.ssm.bean.User;
import com.pw.ssm.bean.UserExample;
import com.pw.ssm.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping(value = {"/index", ""})
    public String index(Model model) {
        UserExample example = new UserExample();
        UserExample.Criteria criteria = example.createCriteria();
        criteria.andIdIsNotNull();
        List<User> users = userService.selectByExample(example);
        model.addAttribute("users", users);
        return "/index";
    }

    @RequestMapping("/updateUser/{id}")
    public String updateUser(Model model,
                             @PathVariable String id) {
        User user = userService.selectByPrimaryKey(Integer.parseInt(id));
        model.addAttribute("user", user);
        return "/updateUser";
    }

    @RequestMapping("/addUser")
    public String addUser() {
        return "/addUser";
    }

    @RequestMapping("/add")
    public String add(Model model,
                      @RequestParam String name,
                      @RequestParam String password) {
        User user = new User();
        UserExample example = new UserExample();
        UserExample.Criteria criteria = example.createCriteria();
        criteria.andNameEqualTo(name);
        User user1 = (User) userService.selectByExample(example);
        if (user1.getName().equals(name)) {
            model.addAttribute("msg", "该昵称已存在！");
            return "redirect:/index";
        }
        user.setName(name);
        user.setPassword(password);
        try {
            userService.insert(user);
        } catch (Exception e) {
            model.addAttribute("msg", "添加失败！");
            e.printStackTrace();
        }
        return "redirect:/index";
    }

    @RequestMapping("/update/{id}")
    public String update(Model model,
                         @PathVariable String id,
                         @RequestParam String name,
                         @RequestParam String password) {
        User user = userService.selectByPrimaryKey(Integer.parseInt(id));
        UserExample example = new UserExample();
        UserExample.Criteria criteria = example.createCriteria();
        criteria.andNameEqualTo(name);
        User user1 = (User) userService.selectByExample(example);
        if (user1.getName().equals(name)) {
            model.addAttribute("msg", "该昵称已存在！");
            return "redirect:/index";
        }
        user.setName(name);
        user.setPassword(password);
        try {
            userService.updateByPrimaryKey(user);
        } catch (Exception e) {
            model.addAttribute("msg", "修改失败！");
            e.printStackTrace();
        }
        return "redirect:/index";
    }

    @RequestMapping("/delUser/{id}")
    public String delUser(Model model,
                          @PathVariable String id) {
        try {
            userService.deleteByPrimaryKey(Integer.parseInt(id));
        } catch (Exception e) {
            model.addAttribute("msg", "删除失败!");
            e.printStackTrace();
        }
        return "redirect:/index";
    }

    @RequestMapping("/getWeather")
    @ResponseBody
    public String getWeather(String city) {
        Map<String, Object> map = new HashMap<>();
        System.out.println("获取天气");
        String u = getWeatherInform(city);
        System.out.println(u);
        return u;
    }

    //根据城市获取天气信息的java代码
    //cityName 是你要取得天气信息的城市的中文名字，如“北京”，“深圳”
    static String getWeatherInform(String cityName) {

        String url = "http://www.weather.com.cn/data/sk/101010100.html ";
        //String url="http://php.weather.sina.com.cn/xml.php?city=%B9%E3%D6%DD&password=DJOYnieT8234jlsK&day=1";
        StringBuffer strBuf;

        strBuf = new StringBuffer();

        try {
            URL oracle = new URL(url);
            URLConnection yc = oracle.openConnection();
            BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream(), "UTF-8"));
            String inputLine = null;
            while ((inputLine = in.readLine()) != null) {
                strBuf.append(inputLine);
            }
            in.close();
        } catch (MalformedURLException e) {
        } catch (IOException e) {
        }
        return strBuf.toString();
    }
}
